package top.infopub.job.wsImpl;


import org.apache.commons.lang3.StringUtils;
import org.quartz.SchedulerException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import top.infopub.common.ErrorCodeEnum;
import top.infopub.dubbo.vo.job.QuartzJobVo;
import top.infopub.dubbo.ws.job.QuartzJobService;
import top.infopub.job.quartz.core.QuartzCoreManager;
import top.infopub.vo.ResponseVo;


/**
 * Quartz job ws接口
 * @author Awoke
 * @version 2018年9月20日
 * @see WsQuartzJobServiceImpl
 * @since
 */
@Service
public class WsQuartzJobServiceImpl implements QuartzJobService {

    private static final Logger log = LoggerFactory.getLogger(WsQuartzJobServiceImpl.class);

    /**
     * quartz_job包名
     */
    private static final String QUARTZ_JOB_PACKAGE = "top.infopub.job.quartz.exec";

    @SuppressWarnings("unchecked")
    @Override
    public ResponseVo<String> addJob(QuartzJobVo addVo) {
        try {
            String jobName = addVo.getJobName();
            String jobBeanName = addVo.getJobBeanName();
            String triggerName = addVo.getTriggerName();
            String jobType = addVo.getJobType();
            // 业务相关数据 原样传递到jobbean中
            String data = addVo.getData();

            // 验参
            if (StringUtils.isBlank(jobName) || StringUtils.isBlank(jobBeanName)
                || StringUtils.isBlank(triggerName) || StringUtils.isBlank(jobType)) {
                return ErrorCodeEnum.ERROR_COMMON_PARAM.getResponseVo();
            }

            // cron job
            if (StringUtils.equals(jobType, "0")) {
                String cronExp = addVo.getCronExp();
                if (StringUtils.isBlank(cronExp)) {
                    return ErrorCodeEnum.ERROR_COMMON_PARAM.getResponseVo();
                }
                QuartzCoreManager.addJob(jobName,
                    Class.forName(QUARTZ_JOB_PACKAGE + "." + jobBeanName), triggerName, cronExp,
                    data);
            }
            // oneTime job 只执行一次
            else if (StringUtils.equals(jobType, "1")) {
                String fireTime = addVo.getFireTime();
                if (StringUtils.isBlank(fireTime)) {
                    return ErrorCodeEnum.ERROR_COMMON_PARAM.getResponseVo();
                }
                QuartzCoreManager.addOneTimeJob(jobName,
                    Class.forName(QUARTZ_JOB_PACKAGE + "." + jobBeanName), triggerName, fireTime,
                    data);
            }
            return ResponseVo.getSuccessResponse();
        }
        catch (SchedulerException e) {
            log.error("", e);
            return ErrorCodeEnum.ERROR_JOB_SCHEDULER.getResponseVo();
        }
        catch (ClassNotFoundException e) {
            log.error("", e);
            return ErrorCodeEnum.ERROR_JOB_NOT_FOUND.getResponseVo();
        }
        catch (Exception e) {
            log.error("", e);
            return ErrorCodeEnum.ERROR_COMMON_SYSTEM.getResponseVo();
        }
    }

    @SuppressWarnings("unchecked")
    @Override
    public ResponseVo<String> updateJob(QuartzJobVo vo) {
        try {
            String jobName = vo.getJobName();
            String triggerName = vo.getTriggerName();
            String jobType = vo.getJobType();
            // 验参
            if (StringUtils.isBlank(jobName) || StringUtils.isBlank(triggerName)
                || StringUtils.isBlank(jobType)) {
                return ErrorCodeEnum.ERROR_COMMON_PARAM.getResponseVo();
            }
            // cron job
            if (StringUtils.equals(jobType, "0")) {
                String cronExp = vo.getCronExp();
                if (StringUtils.isBlank(cronExp)) {
                    return ErrorCodeEnum.ERROR_COMMON_PARAM.getResponseVo();
                }
                QuartzCoreManager.rescheduleJob(triggerName, jobName, cronExp);
            }
            // oneTime job 只执行一次
            else if (StringUtils.equals(jobType, "1")) {
                String fireTime = vo.getFireTime();
                if (StringUtils.isBlank(fireTime)) {
                    return ErrorCodeEnum.ERROR_COMMON_PARAM.getResponseVo();
                }
                QuartzCoreManager.rescheduleOneTimeJob(triggerName, fireTime);
            }
            return ResponseVo.getSuccessResponse();
        }
        catch (SchedulerException e) {
            log.error("", e);
            return ErrorCodeEnum.ERROR_JOB_SCHEDULER.getResponseVo();
        }
        catch (Exception e) {
            log.error("", e);
            return ErrorCodeEnum.ERROR_COMMON_SYSTEM.getResponseVo();
        }
    }

    @SuppressWarnings("unchecked")
    @Override
    public ResponseVo<String> deleteJob(QuartzJobVo vo) {
        try {
            String jobName = vo.getJobName();
            String triggerName = vo.getTriggerName();
            // 验参
            if (StringUtils.isBlank(jobName) || StringUtils.isBlank(triggerName)) {
                return ErrorCodeEnum.ERROR_COMMON_PARAM.getResponseVo();
            }
            QuartzCoreManager.deleteJob(triggerName, jobName);
            return ResponseVo.getSuccessResponse();
        }
        catch (SchedulerException e) {
            log.error("", e);
            return ErrorCodeEnum.ERROR_JOB_SCHEDULER.getResponseVo();
        }
        catch (Exception e) {
            log.error("", e);
            return ErrorCodeEnum.ERROR_COMMON_SYSTEM.getResponseVo();
        }
    }

    @SuppressWarnings("unchecked")
    @Override
    public ResponseVo<String> pauseJob(QuartzJobVo vo) {
        try {
            String jobName = vo.getJobName();
            String triggerName = vo.getTriggerName();
            // 验参
            if (StringUtils.isBlank(jobName) || StringUtils.isBlank(triggerName)) {
                return ErrorCodeEnum.ERROR_COMMON_PARAM.getResponseVo();
            }
            QuartzCoreManager.pauseJob(triggerName, jobName);
            return ResponseVo.getSuccessResponse();
        }
        catch (SchedulerException e) {
            log.error("", e);
            return ErrorCodeEnum.ERROR_JOB_SCHEDULER.getResponseVo();
        }
        catch (Exception e) {
            log.error("", e);
            return ErrorCodeEnum.ERROR_COMMON_SYSTEM.getResponseVo();
        }
    }

    @SuppressWarnings("unchecked")
    @Override
    public ResponseVo<String> resumeJob(QuartzJobVo vo) {
        try {
            String jobName = vo.getJobName();
            String triggerName = vo.getTriggerName();
            // 验参
            if (StringUtils.isBlank(jobName) || StringUtils.isBlank(triggerName)) {
                return ErrorCodeEnum.ERROR_COMMON_PARAM.getResponseVo();
            }
            QuartzCoreManager.resumeJob(triggerName, jobName);
            return ResponseVo.getSuccessResponse();
        }
        catch (SchedulerException e) {
            log.error("", e);
            return ErrorCodeEnum.ERROR_JOB_SCHEDULER.getResponseVo();
        }
        catch (Exception e) {
            log.error("", e);
            return ErrorCodeEnum.ERROR_COMMON_SYSTEM.getResponseVo();
        }
    }

}
